package com.xxxx.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IGeliService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 隔离报备表单 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/geli")
public class GeliController {

    @Autowired
    private IGeliService iGeliService;

    @ApiOperation(value = "获取自己的隔离报备信息")
    @GetMapping("/getone")
    public Geli getOneGeli(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminid",((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        queryWrapper.orderByDesc("stime");
        return iGeliService.getOne(queryWrapper);
    }

    @ApiOperation(value = "添加隔离报备信息")
    @PostMapping("/post")
    public RespBean addGeli(@RequestBody Geli geli){
        geli.setAdminid(((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        if (iGeliService.save(geli)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败!");
    }

    @ApiOperation(value = "更新隔离信息")
    @PutMapping("/update")
    public RespBean updateIGeli(@RequestBody Geli geli){
        geli.setAdminid(((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        if (iGeliService.updateById(geli)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "查询所有的隔离报备（分页）")
    @GetMapping("/getPageGeli/")
    public RespPageBean getInVaccine(@RequestParam(defaultValue = "1") Integer currentPage ,
                                     @RequestParam(defaultValue = "10")Integer size,
                                     Geli geli,
                                     String[] beginDateScope,
                                     String collegename,
                                     String classname,
                                     @RequestParam(defaultValue = "0")int roleid){
        Admin admin = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int type=admin.getRoles().get(0).getId();
        int collegeid;
        if(admin.getCollegeid()!=null){
            collegeid = admin.getCollegeid();
        }else{
            collegeid=0;
        }
        int classid;
        if(admin.getCollegeid()!=null){
            classid = admin.getCollegeid();
        }else{
            classid=0;
        }
        return iGeliService.getGeliByPage(currentPage,size,geli,beginDateScope,type,collegename,collegeid,classname,classid,roleid);
    }


}
